// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import store from './vuex/store'
Vue.use(Vuex)

import Vant from 'vant';
import { Toast } from 'vant';
import 'vant/lib/index.css'
Vue.use(Vant);

import AMap from 'vue-amap';
Vue.use(AMap);

import $ from 'jquery';
import Mint from 'mint-ui';
import 'mint-ui/lib/style.css'
import '../static/font/iconfont.css';
import setTitle from './common/settitle.js'; // 设置页面标题
window.setTitle = setTitle //挂在window的上面。全局可直接使用的额
Vue.use(Mint);

import App from './App'
import router from './router'

import VueCookies from 'vue-cookies'
Vue.use(VueCookies)

// import wx from 'weixin-js-sdk';
// Vue.prototype.wx = wx;
// console.log(wx)

// 初始化vue-amap
AMap.initAMapApiLoader({
    // 高德key
    key: '657b30f6ee334d9dd973450ef17f8d70',
    // 插件集合 （插件按需引入）
    plugin: ['AMap.Geolocation']
});

// 公用组件区
import toLoad from './components/toLoad';
import loading from './components/loading';
import goTop from '@/pages/foot/goTop'
Vue.component('to-load',toLoad);
Vue.component('loading',loading);
Vue.component('goTop',goTop);

// import loading from './common/glodal_func.js'
// Vue.use(loading);



import vuefilter from "./common/vuefilter"
import JsBridge from "./common/JsBridge"
import api from "./common/api"
import base from "./common/base"
import wxjs from 'weixin-js-sdk'
import $axios from "./common/axios"
import JSutil from "./common/JSutil"

//时间过滤器
Vue.filter('stringJoints',function(data,placeholder=0,location=1){
		let str = "";
		data = data.toString();
		if(data.length==1){
			str = '0'+data;
		}else if(data.length==0) {
			str = '00';
		}else if(data.length==2){
			str = data;
		}
		return str;
	})


Date.prototype.format = function(fmt) {
     var o = { 
        "M+" : this.getMonth()+1,                 //月份 
        "d+" : this.getDate(),                    //日 
        "h+" : this.getHours(),                   //小时 
        "m+" : this.getMinutes(),                 //分 
        "s+" : this.getSeconds(),                 //秒 
        "q+" : Math.floor((this.getMonth()+3)/3), //季度 
        "S"  : this.getMilliseconds()             //毫秒 
    }; 
    if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
    }
     for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
         }
     }
    return fmt; 
}


//微信扫描
// import wx from "weixin-jsapi";
// Vue.prototype.wx = wx

Vue.prototype.$api = api;
Vue.prototype.$axios = $axios;
Vue.prototype.$vuefilter = vuefilter;
Vue.prototype.$JsBridge = new JsBridge();
Vue.config.productionTip = false
Vue.prototype.$base = base;
Vue.prototype.$wxjs = wxjs;
Vue.prototype.$JSutil = JSutil;
// console.log(Vue.JSutil)

Vue.directive('anchor', {
    inserted: function(el, binding) {
        el.onclick = function() {
            document.documentElement.scrollTop = $('#anchor-' + binding.value).offset().top
        }
    }
})


/* eslint-disable no-new */
let vue = new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<App/>'
})

//后退		
// Vue.prototype.$retreat = ()=>{
	
// 	if (window.history.length <= 1) {
// 		Vue.$router.push({path:'/'})
// 	} else {
// 		Vue.$router.go(-1);
// 	}
// };


let head = document.getElementsByTagName('head');
let meta1 = document.createElement('meta');

router.beforeEach((to, from, next) => {
    const agent = navigator.userAgent
    const isiOS = !!agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
    console.log(to.meta);
    meta1.name="keywords";

    meta1.content=to.meta.content;
    head[0].appendChild(meta1);
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        var token = localStorage.getItem("token");

        if (token) { // 通过vuex state获取当前的token是否存在
            if (isiOS && to.path !== location.pathname) {
                // 此处不可使用location.replace
                location.assign(to.fullPath)
            } else {
                next()
            }
        } else {
            localStorage.setItem('beforeLoginUrl', to.fullPath);

            next({
                path: '/login',
                query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        if (isiOS && to.path !== location.pathname) {
            // 此处不可使用location.replace
            location.assign(to.fullPath)
        } else {
            next()
        }
    }
});
